package com.mew.slots.core.game.fkymr.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.szxz.dto.SzxzInRoom;
import com.mew.slots.core.game.szxz.message.SzxzBonusPlayResponseMessage;
import org.springframework.stereotype.Service;

/**
 * @author dylin
 * @create 2021/1/29 0029
 */
@Service
public class FkymrBonusService {
    private final int[][] rouletteOptions={
            {1,2,3,-1},
            {2,3,4,-1},
            {4,6,8,10,-1},
            {6,8,10,20,-1},
            {4,6,8,10,20,50,100,150,180,200}
    };
    private final int[][] rouletteProbability={
            {10,20,20,50},
            {10,20,20,50},
            {10,20,40,30,30},
            {10,30,30,30,30},
            {7,20,20,20,20,10,2,1,1,1},
    };
    public SzxzBonusPlayResponseMessage play(StrategyGameVO player){
        SzxzBonusPlayResponseMessage message=new SzxzBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber()<1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }
        SzxzInRoom vo = JSON.parseObject(winFree.getJson(), SzxzInRoom.class);

        long laseBet=winFree.getBet()*winFree.getLastLineNumber();
        //大转盘 清空次数
        if(vo.getRouletteIndex()==4){
            winFree.setBounsNumber(0);
        }
        playGame(laseBet,message,vo);
        player.setBetNumber(0);
        player.setInBetNumber(laseBet);
        player.setWinType(SlotsWinType.Special.code());
        player.setPaymentNumber(message.getWinGold());
        player.setBetTimes((int)(message.getWinGold()/laseBet));
        //向前图标  不减少次数
        if(message.getType()!=1&&winFree.getBounsNumber()>0){
            winFree.setBounsNumber(winFree.getBounsNumber()-1);
        }
        message.setBonusNumber(winFree.getBounsNumber());
        if(winFree.getBounsNumber()==0){
            //重置json
            message.setIsGameOver(1);
            vo.setRouletteIndex(0);
        }
        player.iconResult(new int[]{
             message.getType()
        });
        winFree.setJson(JSON.toJSONString(vo));
        return message;
    }
    private void playGame(long lastBet, SzxzBonusPlayResponseMessage message, SzxzInRoom vo){

        int ri=vo.getRouletteIndex();
        int randomIndex=SlotsGameUtil.weights(rouletteProbability[ri]);
        int randomResult= rouletteOptions[ri][randomIndex];
        if(randomResult<0){// 结果特殊
            message.setType(1);
            vo.setRouletteIndex(vo.getRouletteIndex()+1);
        }else{// 结果为金币奖励
            message.setType(0);
            message.setRate(randomResult);
            message.setWinGold(lastBet*randomResult);
        }
    }

    public static void main(String[] args) {
        StrategyGameVO vo = SlotsGameUtil.buildTestStrategyGameVO();
        WinFree winFree=vo.getWinFree();
        winFree.setJson(JSON.toJSONString(new SzxzInRoom()));
        winFree.setBet(20L);
        winFree.setLastLineNumber(50);
        int allmul=0;
        FkymrBonusService service=new FkymrBonusService();
        for (int i = 0; i < 100000; i++) {
            winFree.setBounsNumber(5);
            int mul=0;
            boolean noOver=true;
            while (noOver){
                SzxzBonusPlayResponseMessage res = service.play(vo);
                if(res.getType()==0){
                    mul+=res.getRate();
                }
                noOver=res.getIsGameOver()==0;

            }
            System.out.println(mul);
            allmul+=mul;
        }
        System.out.println("平均:"+allmul/100000.0);
    }
}
